<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class UsersService extends MY_Controller {
	public function __construct(){
		parent::__construct();

		if($this->isAdmin() == false){
			redirect('home/notaccess');
		}

		$this->load->helper(array('form','url'));
		$this->load->model('User_model','User');
        $this->load->model('AdminUser_model', 'AdminUser');
	}

    /**
     * get page list of user
     */
	public function pageList(){
		$pageindex = $this->input->get("pageindex");
		$pagesize = $this->input->get("pagesize");
        $user_name = $this->input->get("user_name");
        $phone = $this->input->get("phone");
        $parent_user_name = $this->input->get("parent_user_name");

		$parameters = array( "pageindex" => $pageindex, "pagesize" => $pagesize,
            "user_name" => $user_name, "phone" => $phone, 'parent_user_name' => $parent_user_name);
		$retVal = $this->User->pageList( $parameters );
		echo(json_encode( $retVal ));
	}

    /**
     * get page list of user
     */
	public function pageListReport(){
		$pageindex = $this->input->get("pageindex");
		$pagesize = $this->input->get("pagesize");
        $from_user_name = $this->input->get("from_user_name");
        $to_user_name = $this->input->get("to_user_name");

		$parameters = array( "pageindex" => $pageindex, "pagesize" => $pagesize, 'from_user_name' => $from_user_name, 'to_user_name' => $to_user_name);
		$retVal = $this->UserReport->pageList( $parameters );
		echo(json_encode( $retVal ));
	}

    public function getAdminUsers(){
        $users = $this->AdminUser->getManyByCondition( '*', array("role" => "admin"), array("id", "asc") );
        echo(json_encode(array( "code" => "success", 'records' => $users)));
    }

    public function getChildren(){
        $id = $this->input->get('id',true);
        $users = $this->User->getManyByCondition( '*', array('parent_id' => $id), null );

        echo(json_encode(array( "code" => "success", 'records' => $users)));
    }

	/**
	 * getAll
	 */
	public function getAll(){
        $users = $this->User->getManyByCondition( '*', array(), array("id", "asc") );
        echo(json_encode(array( "code" => "success", 'data' => $users)));
	}

    /**
     * getAllReport
     */
	public function getAllReport(){
        $list = $this->UserReport->getManyByCondition( '*', array(), array("id", "asc") );
        echo(json_encode(array( "code" => "success", 'data' => $list)));
	}

    /**
     * getAllReward
     */
	public function getAllReward(){
        $list = $this->UserReward->getManyByCondition( '*', array(), array("id", "asc") );
        echo(json_encode(array( "code" => "success", 'data' => $list)));
	}

    public function changePasssword(){
        $ori = $this->input->post('ori',true);
		$newpas = $this->input->post('newpas',true);

        $username = $this->getLoginUsername();
        $users = $this->User->getManyByCondition( '*', array('user_name' => $username, 'pass' => $ori, 'role' => 'admin'), null );

        if( count($users) == 0 ){
            echo(json_encode(array( "code" => "error", "message" => "原密码错误!" )));
            return;
        }

        $this->User->update_common_by_array( array( 'user_name' => $username, 'role' => 'admin' ),
               array( 'pass' => $newpas ) );

        echo(json_encode(array( "code" => "success" )));
    }

    public function changeUserPasssword(){
        $id = $this->input->post('id',true);
		$newpas = $this->input->post('newpas',true);

        $this->User->update_common_by_array( array( 'id' => $id ), array( 'pass' => $newpas ) );

        echo(json_encode(array( "code" => "success" )));
    }

    public function memberEdit(){
        $id = $this->input->post('id',true);
        $user_name = $this->input->post('user_name',true);
		$phone = $this->input->post('phone',true);
        $child_count = $this->input->post('child_count',true);
        $total_child_count = $this->input->post('total_child_count',true);

        $this->User->update_common_by_array( array( 'id' => $id ), array(
                'user_name' => $user_name,
                'phone' => $phone,
                'child_count' => $child_count,
                'total_child_count' => $total_child_count,
            ) );

        echo(json_encode(array( "code" => "success" )));
    }

    public function change_parent(){
        $id = $this->input->post('id',true);
        $phone = $this->input->post('phone',true);

        $users = $this->User->getManyByCondition( '*', array('phone' => $phone), null );
        if( count($users) == 0 ){
            echo(json_encode(array( "code" => "error", "message" => '手机号不存在')));
        }
        else{
            $this->User->update_common_by_array( array( 'id' => $id ), array(
                    'parent_id' => $users[0]['id'],
                    'parent_user_name' => $users[0]['user_name']
                ) );

            echo(json_encode(array( "code" => "success" )));
        }
    }

    public function get_user_by_phone(){
        $phone = $this->input->get('phone',true);

        $users = $this->User->getManyByCondition( '*', array('phone' => $phone), null );
        if( count($users) == 0 ){
            echo(json_encode(array( "code" => "error", "message" => '手机号不存在')));
        }
        else{

            echo(json_encode(array( "code" => "success", 'message' => '用户名为：'.$users[0]['user_name'] )));
        }
    }

    public function disable_user(){
        $id = $this->input->post('id',true);
        $this->User->update_common_by_array( array( 'id' => $id ), array('status' => -1) );

        $users = $this->User->getManyByCondition( '*', array('id' => $id), null );

        $this->User->update_common_by_array( array( 'parent_id' => $id ), array('parent_id' => $users[0]['parent_id']) );

        echo(json_encode(array( "code" => "success" )));
    }
}